<template>
  <div class="login-container">
    <div class="login-box">
      <div class="avator-box">
        <img src="http://weldon.net.cn/back/img/logo2.fc048282.png" alt="" />
      </div>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="form.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="form.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submit">登录</el-button>
          <el-button type="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginApi } from "@/api/user";
// import { Message } from 'element-ui'
export default {
  name: "LoginIndex",
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" },
          {
            min: 5,
            max: 11,
            trigger: "blur",
            message: "长度在 5 到 11 个字符",
          },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入密码" },
          { min: 6, max: 10, trigger: "blur", message: "密码在6-10位" },
        ],
      },
    };
  },
  watch: {},
  methods: {
    // 重置
    reset() {
      this.$refs.form.resetFields();
    },
    // 登录
    async submit() {
      try {
        // 添加登录提交的校验
        await this.$refs.form.validate();
        const res = await loginApi(this.form);
        // console.log(res.data.token);
        this.$store.commit("user/setToken", res.data.token);
        this.$router.push("/");
      } catch (e) {
        // console.log(e);
        console.log("校验失败");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #2b4b6b;
  .login-box {
    position: relative;
    width: 450px;
    height: 300px;
    background-color: #fff;
    .avator-box {
      width: 130px;
      height: 130px;
      margin: 0 auto;
      padding: 5px;
      border-radius: 50%;
      border: 1px solid #eee;
      box-shadow: 0 0 10px #ddd;
      background-color: #fff;
      transform: translateY(-50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .el-form {
      position: absolute;
      bottom: 0;
      padding: 0 20px;
      width: 450px;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
